package org.fs;

import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

import org.fs.dao.FlightHome;
import org.fs.entities.FsConnection;
import org.htmlparser.NodeFilter;
import org.htmlparser.beans.FilterBean;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.InputTag;

public class FlightScanner {

	private static Pattern flightsTo = Pattern.compile("\\\"o\":\\[[^\\]]*");
	private static Pattern flightsBack = Pattern.compile("\\\"i\":\\[[^\\]]*");
	private static Pattern flightPattern = Pattern.compile("\\\"d\\\"[^\\}]*");
	private static final String SKYSCANNER_URL = "http://www.skyscanner.net/flights/";
	private FlightHome flightHome;
	private ConnectionManager connectionManager;
	private AirportAbbreviationMap airportMap;
	String[] months = { "march-2009", "april-2009", "may-2009", "june-2009",
			"july-2009", "august-2009", "september-2009", "october-2009",
			"november-2009", "december-2009" };

	public void run() {
		airportMap.init();
		FlightWrapper.setAam(airportMap);

		List<FsConnection> conns = connectionManager.getAllConnections();
		for (int i = 0; i < conns.size(); i++) {
			FsConnection connection = conns.get(i);
			String from = connection.getAirportByAId1().getAbbreviation();
			String to = connection.getAirportByAId2().getAbbreviation();

			System.out.println("[" + i + "/" + conns.size() + "]");
			System.out.println(from.toUpperCase() + "-" + to.toUpperCase());
			
			for (int j = 0; j < months.length; j++) {				
				String month = months[j];
				
				FilterBean fb = new FilterBean();
				fb.setFilters(new NodeFilter[] { new HasAttributeFilter("id",
						"flexibledates_data") });
				fb.setURL(getUrl(from, to, month));

				InputTag in = (InputTag) fb.getNodes().elementAt(0);
				String data = in.getAttributeEx("value").getValue();

				Scanner scanner = new Scanner(data);
				Scanner scannerTo = new Scanner(scanner.findInLine(flightsTo));
				Scanner scannerBack = new Scanner(scanner.findInLine(flightsBack));

				processData(scannerTo, from, to);
				processData(scannerBack, to, from);
			}
		}

	}
	
	private String getUrl(String from, String to, String month){
		return SKYSCANNER_URL + from + "/" + to + "/" + month
		+ "/" + month + "/";
	}

	private static String get(String[] params, int i) {
		String[] tmp = params[i].split(":");
		return tmp[1].substring(1, tmp[1].length() - 1);
	}

	private void processData(Scanner scn, String from, String to) {
		String string;
		while ((string = scn.findInLine(flightPattern)) != null) {
			String[] params = string.split(",");
			if (params.length != 6 || get(params, 1).equals("0"))
				continue;
			FlightWrapper flight = new FlightWrapper();
			flight.setDay(get(params, 0));
			flight.setPrice(get(params, 2));
			flight.setId(get(params, 3));
			flight.setDeparture(get(params, 4));
			flight.setArrival(get(params, 5));
			flight.setFrom(from.toUpperCase());
			flight.setTo(to.toUpperCase());

//			System.out.println(flight.toString());
			flightHome.persist(flight.getWrapped());
		}
	}

	public void setAirportMap(AirportAbbreviationMap airportMap) {
		this.airportMap = airportMap;
	}

	public void setFlightHome(FlightHome flightHome) {
		this.flightHome = flightHome;
	}

	public void setConnectionManager(ConnectionManager connectionManager) {
		this.connectionManager = connectionManager;
	}

}
